<?php

namespace ctrl;

use model\admin_user;
use model\admin_group;
use libs\view;

class adminuser extends base
{
	private static $M, $MG;
	public static function Init()
	{
		parent::init();
		self::$M = new admin_user;
		self::$MG = new admin_group;
	}

	public static function index()
	{
		if (isset($_GET['list_json'])) {
			jsonList(self::$M->jsonPageSelect());
		} else {
			view::display();
		}
	}

	public static function add()
	{
		if (isset($_GET['view'])) {
			view::assign('info', null);
			view::assign('infoId', null);
			view::assign('title', "添加管理员");
			view::assign('groupList', self::$MG->selectGroup());
			view::display("post");
		} else {
			$result = self::$M->insertData();
			Response(['status' => $result['status'], 'info' => $result['msg']]);
		}
	}


	public static function edit()
	{
		if (isset($_GET['view'])) {
			$info = self::$M->findData();
			view::assign('groupList', self::$MG->selectGroup());
			view::assign('info', $info);
			view::assign('infoId', $info['id']);
			view::assign('title', "编辑管理员");
			view::display("post");
		} else if (isset($_GET['switchStatus'])) {
			$result = self::$M->switchStatus();
			Response(['status' => $result['status'], 'info' => $result['msg']]);
		} else {
			$result = self::$M->updateData();
			Response(['status' => $result['status'], 'info' => $result['msg']]);
		}
	}

	public static function del()
	{
		$result = self::$M->deleteOneData();
		Response(['status' => $result['status'], 'info' => $result['msg']]);
	}


	public static function passwordedit()
	{
		if (isset($_GET['view'])) {
			view::assign('info', self::$M->findData());
			view::display();
		} else {
			$result = self::$M->passwordEdit();
			Response(['status' => $result['status'], 'info' => $result['msg']]);
		}
	}
}
